#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, m;
int f[N];
void init() { rep(i, 1, n) f[i] = i; }
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
void merge(int x, int y) {
  int fx = find(x), fy = find(y);
  if (fx != fy) f[fy] = fx;
}
bool diff(int x, int y) { return find(x) != find(y); }
bool check(vector<pii>& a) {
  for (pii p : a) {
    if (!diff(p.first, p.second)) return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) {
    cin >> m;
    n = m * 2;
    init();
    map<int, int> mp;
    int tot = 0;
    vector<pii> a;
    rep(i, 1, m) {
      int e, x, y;
      cin >> x >> y >> e;
      if (!mp.count(x)) mp[x] = ++tot;
      if (!mp.count(y)) mp[y] = ++tot;
      x = mp[x], y = mp[y];
      if (e)
        merge(x, y);
      else
        a.push_back(pii(x, y));
    }
    if (check(a))
      cout << "YES\n";
    else
      cout << "NO\n";
  }
  return 0;
}